<template>
    <el-container class="home-container">
        <!--头部区域-->
        <el-header>
            <div>
                <img src="" alt="">
                <span>商城后台管理系统</span>
            </div>
            <el-button type="info" @click="logout">退出</el-button>
        </el-header>
        <!--页面主体区域-->
        <el-container>
            <!--侧边栏-->
            <el-aside :width="isCollapse?'64px':'200px'">
                <!--折叠按钮-->
                <div class="toggle-button" @click="toggleCollapse">|||</div>
                <!--侧边栏-->
                <el-menu 
                    background-color="#373D41" 
                    text-color="#fff" 
                    active-text-color="#409eff"
                    unique-opened
                    :collapse="isCollapse"
                    :collapse-transition="false"
                    router
                    :default-active="activePath">
                >
                    <!--一级菜单-->
                    <el-submenu :index="index+''" v-for="(item,index) in menuList" :key="index">
                        <!--一级菜单模板区-->
                        <template slot="title">
                            <i :class="iconsObj[item.id]"></i>
                            <span>{{item.authName}}</span>
                        </template>

                        <!--二级菜单-->
                        <el-menu-item :index="'/'+subItem.path" v-for="(subItem,subIndex) in item.children" 
                            :key="subIndex"  @click="saveNavStatue('/'+subItem.path)" >
                            <template slot="title">
                                <i class="el-icon-menu"></i>
                                <span>{{subItem.authName}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!--右侧主体内容-->
            <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
//方法
import {getMenus} from '../request/home'
export default {
    components: {

    },
    data() {
        return {
            menuList:null,
            //图标
            iconsObj:{
                '125':'iconfont icon-user',
                '103':'iconfont icon-tijikongjian',
                '101':'iconfont icon-shangpin',
                '102':'iconfont icon-danju',
                '145':'iconfont icon-baobiao'
            },
            isCollapse:false,
            activePath:''
        }
    },
    computed: {

    },
    created() {
        this.activePath = window.sessionStorage.getItem('activePath')
        this.getAllMenus()
    },
    mounted() {
        
    },
    methods: {
        async getAllMenus(){
           const res =  await getMenus()
           if(res.meta.status==200){
               this.menuList = res.data
           }else{
               this.$message.error('请求导航栏数据失败！')
           }
        },
        logout(){
            this.$router.push('/login')
            window.sessionStorage.clear()
        },
        /*
            菜单折叠与展开
        */
        toggleCollapse(){
            this.isCollapse = !this.isCollapse
        },
        /*
            保存链接的激活状态
        */
       saveNavStatue(activePath){
           window.sessionStorage.setItem('activePath',activePath)
           this.activePath = activePath
       }
    }
}
</script>

<style lang='less' scoped>
.home-container{
  width: 100%;
  height: 100%;   
}
.el-header{
    background-color: #373D41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #fff;
    font-size: 20px;
    padding-left: 0; /* 去掉左侧边距 */
    > div{
        display: flex;
        align-items: center;
        span{
            margin-left: 15px;
        }
    }
}
.el-aside{
    background-color: #333744;
    .el-menu{
        border-right: none;
    }
}
.el-main{
    background-color: #EAEDF1;
}
.iconfont{
    margin-right: 10px;
}

.toggle-button{
    background-color: #4a5064;
    font-size: 10px;
    color: #fff;
    line-height: 24px;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
}
</style>
